﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;

using ApplicationLibrary;
using ApplicationLibrary.ExtendedControls;
using ApplicationLibrary.SqlConnector;
using ApplicationLibrary.DBManager;
using System.Windows.Forms;

namespace Plumber.Database
{
    internal class RadarMasterParam : RadarParameters
    {
        public RadarMasterParam(string type, string code)
        {
            Params = new List<object>();
            Params.Add(type);
            Params.Add(code);
        }
    }

    internal class RadarTitleParam : RadarParameters
    {
        public RadarTitleParam(string code)
        {
            Params = new List<object>();
            Params.Add(code);
        }
    }

    internal class RadarTemplatesParam : RadarParameters
    {
        public RadarTemplatesParam(string code)
        {
            Params = new List<object>();
            Params.Add(code);
        }
    }

    internal class RadarTitlesParam : RadarParameters
    {
        public RadarTitlesParam(string code)
        {
            Params = new List<object>();
            Params.Add(code);
        }
    }

    internal class RadarMasterTypesParam : RadarParameters
    {
        public RadarMasterTypesParam(string code)
        {
            Params = new List<object>();
            Params.Add(code);
        }
    }

    internal class RadarSentencesParam : RadarParameters
    {
        public RadarSentencesParam(string code)
        {
            Params = new List<object>();
            Params.Add(code);
        }
    }

    #region Radar RadarMasterForm
    internal class RadarMasterForm : RadarForm
    {
        private QueryBuilder qb = new QueryBuilder();
        private SqlABParameter p1;
        private SqlABParameter p2;
        private SqlABParameter p3;

        public RadarMasterForm()
            : base()
        {
            rdrCodeColumn = PL_Masters.Code.Name;
            rdrDescColumn = PL_Masters.Description.Name;
            rdrNameSpace = "Plumber.Plumber.Masters.Forms.Masters";
        }

        protected override Table GetRadarTable()
        {
            return new PL_Masters();
        }

        protected override bool DefineFindQuery(SqlABCommand sqlCmd)
        {
            p1 = new SqlABParameter("@p1", PL_Masters.Code);
            p2 = new SqlABParameter("@p2", PL_Masters.Type);

            qb.Clear();
            qb.AddSelectAll(PL_Masters.Name);
            qb.AddFrom(PL_Masters.Name);
            qb.AddCompare(PL_Masters.Code, p1);
            qb.AddCompare(PL_Masters.Type, p2);


            sqlCmd.CommandText = qb.Query;
            sqlCmd.Parameters.Add(p1);
            sqlCmd.Parameters.Add(p2);

            return true;
        }

        protected override void PrepareFindQuery(IRadarParameters param)
        {
            p1.Value = param[0];
            p2.Value = param[1];
        }

        protected override void OnFound(SqlABDataReader sqlReader)
        {
            Description = sqlReader[PL_Masters.Description.Name].ToString();
        }

        protected override string DefineBrowseQuery(SqlABCommand sqlCmd)
        {
            p3 = new SqlABParameter("@p3", SqlDbType.Int);
            qb.Clear();
            qb.AddSelectAll(PL_Masters.Name);
            qb.AddFrom(PL_Masters.Name);
            qb.AddCompare(PL_Masters.Disabled, "@p3");

            sqlCmd.Parameters.Add(p3);

            return qb.Query;
        }

        protected override void PrepareBrowseQuery()
        {
            p3.Value = 0;
        }

        protected override IRadarParameters PrepareRadarParameters(DataGridViewRow row)
        {
           string  mt = DataGridViewRowValues.GetValue<string>(row, PL_Masters.Type);
           string Code = DataGridViewRowValues.GetValue<string>(row, PL_Masters.Code);
           return new RadarMasterParam(mt, Code);
        }

        public override IRadarParameters GetRadarParameters(string text)
        {
            throw new NotImplementedException();
        }

    }
    #endregion

    #region Radar RadarMasterFormByType
    internal class RadarMasterFormByType : RadarForm
    {
        private QueryBuilder qb = new QueryBuilder();
        private SqlABParameter pb1;
        private SqlABParameter pb2;
        private SqlABParameter pf1;
        private SqlABParameter pf2;

        string type = string.Empty;

        public RadarMasterFormByType(string type)
            : base()
        {
            rdrCodeColumn = PL_Masters.Code.Name;
            rdrDescColumn = PL_Masters.Description.Name;
            rdrNameSpace = "Plumber.Plumber.Masters.Forms.Masters";
            this.type = type;
        }

        public void SetFilterType(string type)
        {
            this.type = type;
        }

        protected override Table GetRadarTable()
        {
            return new PL_Masters();
        }

        protected override bool DefineFindQuery(SqlABCommand sqlCmd)
        {
            pf1 = new SqlABParameter("@pf1", PL_Masters.Code);
            pf2 = new SqlABParameter("@pf2", PL_Masters.Type);

            qb.Clear();
            qb.AddSelectAll(PL_Masters.Name);
            qb.AddFrom(PL_Masters.Name);
            qb.AddCompare(PL_Masters.Code, pf1);
            qb.AddCompare(PL_Masters.Type, pf2);


            sqlCmd.CommandText = qb.Query;
            sqlCmd.Parameters.Add(pf1);
            sqlCmd.Parameters.Add(pf2);

            return true;
        }

        protected override void PrepareFindQuery(IRadarParameters param)
        {
            pf1.Value = param[1];
            pf2.Value = param[0];
        }

        protected override void OnFound(SqlABDataReader sqlReader)
        {
            Description = sqlReader[PL_Masters.Description.Name].ToString();
        }

        protected override string DefineBrowseQuery(SqlABCommand sqlCmd)
        {
            pb1 = new SqlABParameter("@pb1", PL_Masters.Disabled);
            pb2 = new SqlABParameter("@pb2", PL_Masters.Type);
            qb.Clear();
            qb.AddSelectAll(PL_Masters.Name);
            qb.AddFrom(PL_Masters.Name);
            qb.AddCompare(PL_Masters.Disabled, pb1);
            qb.AddCompare(PL_Masters.Type, pb2);

            sqlCmd.Parameters.Add(pb1);
            sqlCmd.Parameters.Add(pb2);

            return qb.Query;
        }

        protected override void PrepareBrowseQuery()
        {
            pb1.Value = 0;
            pb2.Value = type;
        }

        protected override IRadarParameters PrepareRadarParameters(DataGridViewRow row)
        {
            string mt = DataGridViewRowValues.GetValue<string>(row, PL_Masters.Type);
            string Code = DataGridViewRowValues.GetValue<string>(row, PL_Masters.Code);
            return new RadarMasterParam(mt, Code);
        }

        public override IRadarParameters GetRadarParameters(string text)
        {
            return new RadarMasterParam(type, text);
        }

    }
    #endregion

    #region Radar Titles
    internal class RadarTitles : RadarForm
    {
        private QueryBuilder qb = new QueryBuilder();
        private SqlABParameter p1;
        private SqlABParameter p2;

        public RadarTitles()
            : base()
        {
            rdrCodeColumn = PL_Titles.Title.Name;
            rdrDescColumn = PL_Titles.Description.Name;
            rdrNameSpace = "Plumber.Plumber.Masters.Forms.Titles";
        }

        protected override Table GetRadarTable()
        {
            return new PL_Titles();
        }

        protected override bool DefineFindQuery(SqlABCommand sqlCmd)
        {
            qb.Clear();
            qb.AddSelectAll(PL_Titles.Name);
            qb.AddFrom(PL_Titles.Name);
            qb.AddCompare(PL_Titles.Title, "@p1");

            p1 = new SqlABParameter("@p1", PL_Titles.Title);

            sqlCmd.CommandText = qb.Query;
            sqlCmd.Parameters.Add(p1);

            return true;
        }

        protected override void PrepareFindQuery(IRadarParameters param)
        {
            //p1.Value = code;
        }

        protected override void OnFound(SqlABDataReader sqlReader)
        {
            Description = sqlReader[PL_Titles.Description.Name].ToString();
        }

        protected override string DefineBrowseQuery(SqlABCommand sqlCmd)
        {
            p2 = new SqlABParameter("@p2", PL_Titles.Title);

            qb.Clear();
            qb.AddSelectAll(PL_Titles.Name);
            qb.AddFrom(PL_Titles.Name);

            sqlCmd.Parameters.Add(p2);

            return qb.Query;
        }

        protected override void PrepareBrowseQuery()
        {
            p2.Value = PL_Titles.Title.DefaultValue;
        }

        protected override IRadarParameters PrepareRadarParameters(DataGridViewRow row)
        {
            return new RadarTitleParam(DataGridViewRowValues.GetValue<string>(row, PL_Titles.Title));
        }

        public override IRadarParameters GetRadarParameters(string text)
        {
            return new RadarTitleParam(text);
        }
    }
    #endregion

    #region Radar Templates
    internal class RadarTemplates : RadarForm
    {
        private QueryBuilder qb = new QueryBuilder();
        private SqlABParameter p1;
        private SqlABParameter p2;

        public RadarTemplates()
            : base()
        {
            rdrCodeColumn = PL_Templates.Code.Name;
            rdrDescColumn = "";
            rdrNameSpace = "Plumber.Plumber.Sales.Forms.Templates";
        }

        protected override Table GetRadarTable()
        {
            return new PL_Templates();
        }

        protected override bool DefineFindQuery(SqlABCommand sqlCmd)
        {
            p1 = new SqlABParameter("@p1", PL_Templates.Code);
            
            qb.Clear();
            qb.AddSelectAll(PL_Templates.Name);
            qb.AddFrom(PL_Templates.Name);
            qb.AddCompare(PL_Templates.Code, p1);

            sqlCmd.CommandText = qb.Query;
            sqlCmd.Parameters.Add(p1);

            return true;
        }

        protected override void PrepareFindQuery(IRadarParameters param)
        {
            p1.Value = param[0];
        }

        protected override void OnFound(SqlABDataReader sqlReader)
        {
            Description = sqlReader[PL_Templates.Description.Name].ToString();
        }

        protected override string DefineBrowseQuery(SqlABCommand sqlCmd)
        {
            p2 = new SqlABParameter("@p2", PL_Templates.Code);
            qb.Clear();
            qb.AddSelectAll(PL_Templates.Name);
            qb.AddFrom(PL_Templates.Name);

            return qb.Query;
        }

        protected override void PrepareBrowseQuery()
        {
            p2.Value = PL_Templates.Code.DefaultValue;
        }

        protected override IRadarParameters PrepareRadarParameters(DataGridViewRow row)
        {
            return new RadarTemplatesParam(DataGridViewRowValues.GetValue<string>(row, PL_Templates.Code));
        }

        public override IRadarParameters GetRadarParameters(string text)
        {
            return new RadarTemplatesParam(text);
        }
    }
    #endregion

    #region Radar Sentences
    internal class RadarSentences : RadarForm
    {
        private QueryBuilder qb = new QueryBuilder();
        private SqlABParameter p1;
        private SqlABParameter p2;

        public RadarSentences()
            : base()
        {
            rdrCodeColumn = PL_Sentences.ID.Name;
            rdrDescColumn = PL_Sentences.Title.Name;
            rdrNameSpace = "Plumber.Plumber.Sales.Forms.Sentences";
        }

        protected override Table GetRadarTable()
        {
            return new PL_Sentences();
        }

        protected override bool DefineFindQuery(SqlABCommand sqlCmd)
        {
            p1 = new SqlABParameter("@p1", PL_Sentences.ID);

            qb.Clear();
            qb.AddSelectAll(PL_Sentences.Name);
            qb.AddFrom(PL_Sentences.Name);
            qb.AddCompare(PL_Sentences.ID, p1);

            sqlCmd.CommandText = qb.Query;
            sqlCmd.Parameters.Add(p1);

            return true;
        }

        protected override void PrepareFindQuery(IRadarParameters param)
        {
            p1.Value = param[0];
        }

        protected override void OnFound(SqlABDataReader sqlReader)
        {
            Description = sqlReader[PL_Sentences.Text.Name].ToString();
        }

        protected override string DefineBrowseQuery(SqlABCommand sqlCmd)
        {
            p2 = new SqlABParameter("@p2", PL_Sentences.ID);
            qb.Clear();
            qb.AddSelectAll(PL_Sentences.Name);
            qb.AddFrom(PL_Sentences.Name);

            return qb.Query;
        }

        protected override void PrepareBrowseQuery()
        {
        }

        protected override IRadarParameters PrepareRadarParameters(DataGridViewRow row)
        {
            return new RadarSentencesParam(DataGridViewRowValues.GetValue<string>(row, PL_Sentences.ID));
        }

        public override IRadarParameters GetRadarParameters(string text)
        {
            return new RadarSentencesParam(text);
        }
    }
    #endregion

    #region Radar Titles
    internal class RadarMasterTypes : RadarForm
    {
        private QueryBuilder qb = new QueryBuilder();
        private SqlABParameter p1;
        private SqlABParameter p2;

        public RadarMasterTypes()
            : base()
        {
            rdrCodeColumn = PL_MasterType.Code.Name;
            rdrDescColumn = PL_MasterType.Description.Name;
            rdrNameSpace = "Plumber.Plumber.Masters.Forms.MasterType";
        }

        protected override Table GetRadarTable()
        {
            return new PL_MasterType();
        }

        protected override bool DefineFindQuery(SqlABCommand sqlCmd)
        {
            p1 = new SqlABParameter("@p1", PL_MasterType.Code);

            qb.Clear();
            qb.AddSelectAll(PL_MasterType.Name);
            qb.AddFrom(PL_MasterType.Name);
            qb.AddCompare(PL_MasterType.Code, p1);

            sqlCmd.CommandText = qb.Query;
            sqlCmd.Parameters.Add(p1);

            return true;
        }

        protected override void PrepareFindQuery(IRadarParameters param)
        {
            p1.Value = param[0];
        }

        protected override void OnFound(SqlABDataReader sqlReader)
        {
            Description = sqlReader[PL_MasterType.Description.Name].ToString();
        }

        protected override string DefineBrowseQuery(SqlABCommand sqlCmd)
        {
            p2 = new SqlABParameter("@p2", PL_Titles.Title);

            qb.Clear();
            qb.AddSelectAll(PL_MasterType.Name);
            qb.AddFrom(PL_MasterType.Name);

            sqlCmd.Parameters.Add(p2);

            return qb.Query;
        }

        protected override void PrepareBrowseQuery()
        {
            p2.Value = PL_MasterType.Code.DefaultValue;
        }

        protected override IRadarParameters PrepareRadarParameters(DataGridViewRow row)
        {
            return new RadarMasterTypesParam(DataGridViewRowValues.GetValue<string>(row, PL_MasterType.Code));
        }
        public override IRadarParameters GetRadarParameters(string text)
        {
            return new RadarMasterTypesParam(text);
        }
    }
    #endregion
}
